{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:28:03.782394Z",
     "iopub.status.busy": "2024-02-01T06:28:03.781398Z",
     "iopub.status.idle": "2024-02-01T06:28:04.315310Z",
     "shell.execute_reply": "2024-02-01T06:28:04.312430Z",
     "shell.execute_reply.started": "2024-02-01T06:28:03.782394Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\n",
      "version 18.0\n"
     ]
    }
   ],
   "source": [
    "clear all\n",
    "capture log close\n",
    "\n",
    "version"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:28:04.321282Z",
     "iopub.status.busy": "2024-02-01T06:28:04.321282Z",
     "iopub.status.idle": "2024-02-01T06:28:04.936829Z",
     "shell.execute_reply": "2024-02-01T06:28:04.934805Z",
     "shell.execute_reply.started": "2024-02-01T06:28:04.321282Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(8 vars, 48 obs)\n"
     ]
    }
   ],
   "source": [
    "insheet using \"ecm.csv\", comma clear"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Inspect data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:28:04.943348Z",
     "iopub.status.busy": "2024-02-01T06:28:04.943348Z",
     "iopub.status.idle": "2024-02-01T06:28:05.554735Z",
     "shell.execute_reply": "2024-02-01T06:28:05.552701Z",
     "shell.execute_reply.started": "2024-02-01T06:28:04.943348Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "    Variable |        Obs        Mean    Std. dev.       Min        Max\n",
      "-------------+---------------------------------------------------------\n",
      "       month |          0\n",
      "n_total_ce~d |         48    8703.063    8278.096        395      29478\n",
      "n_total_we~d |         48    2348.292    2316.869        224      10598\n",
      "pct_interc~d |         48    .2797099    .1891446   .0152781   .8329114\n",
      "pct_ar~l_med |         48    .6884117    .2004862   .1518987   .9828756\n",
      "-------------+---------------------------------------------------------\n",
      "pct_ar~n_med |         48    .9788974    .0242661   .9045045          1\n",
      "pct_de~l_med |         48    .0318784    .0320325   .0017017   .1532258\n",
      "pct_de~n_med |         48    .0211026    .0242661          0   .0954955\n"
     ]
    }
   ],
   "source": [
    "summarize"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:28:05.557242Z",
     "iopub.status.busy": "2024-02-01T06:28:05.557242Z",
     "iopub.status.idle": "2024-02-01T06:28:06.257812Z",
     "shell.execute_reply": "2024-02-01T06:28:06.255774Z",
     "shell.execute_reply.started": "2024-02-01T06:28:05.557242Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "month                          pct_arrivals_central_med\n",
      "n_total_central_med            pct_arrivals_western_med\n",
      "n_total_western_med            pct_deaths_central_med\n",
      "pct_interceptions_central_med  pct_deaths_western_med\n"
     ]
    }
   ],
   "source": [
    "ds, varwidth(32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:28:06.260800Z",
     "iopub.status.busy": "2024-02-01T06:28:06.260800Z",
     "iopub.status.idle": "2024-02-01T06:28:07.072679Z",
     "shell.execute_reply": "2024-02-01T06:28:07.072679Z",
     "shell.execute_reply.started": "2024-02-01T06:28:06.260800Z"
    }
   },
   "outputs": [],
   "source": [
    "rename n_total_western_med      n_total\n",
    "rename pct_arrivals_central_med pct_arrivals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:28:07.072679Z",
     "iopub.status.busy": "2024-02-01T06:28:07.072679Z",
     "iopub.status.idle": "2024-02-01T06:28:07.868849Z",
     "shell.execute_reply": "2024-02-01T06:28:07.866846Z",
     "shell.execute_reply.started": "2024-02-01T06:28:07.072679Z"
    }
   },
   "outputs": [],
   "source": [
    "gen total_thousands = n_total/1000\n",
    "drop n_total"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Generate variables"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:28:07.869952Z",
     "iopub.status.busy": "2024-02-01T06:28:07.869952Z",
     "iopub.status.idle": "2024-02-01T06:28:08.665869Z",
     "shell.execute_reply": "2024-02-01T06:28:08.663841Z",
     "shell.execute_reply.started": "2024-02-01T06:28:07.869952Z"
    }
   },
   "outputs": [],
   "source": [
    "gen date = mofd(date(month, \"YMD\"))\n",
    "format date %tm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:28:08.667863Z",
     "iopub.status.busy": "2024-02-01T06:28:08.667863Z",
     "iopub.status.idle": "2024-02-01T06:28:09.465160Z",
     "shell.execute_reply": "2024-02-01T06:28:09.462058Z",
     "shell.execute_reply.started": "2024-02-01T06:28:08.667863Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Time variable: date, 2016m1 to 2019m12\n",
      "        Delta: 1 month\n"
     ]
    }
   ],
   "source": [
    "tsset date, monthly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:28:09.467064Z",
     "iopub.status.busy": "2024-02-01T06:28:09.467064Z",
     "iopub.status.idle": "2024-02-01T06:28:10.260775Z",
     "shell.execute_reply": "2024-02-01T06:28:10.259854Z",
     "shell.execute_reply.started": "2024-02-01T06:28:09.467064Z"
    }
   },
   "outputs": [],
   "source": [
    "sort date"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:28:10.263764Z",
     "iopub.status.busy": "2024-02-01T06:28:10.262769Z",
     "iopub.status.idle": "2024-02-01T06:28:11.072557Z",
     "shell.execute_reply": "2024-02-01T06:28:11.072557Z",
     "shell.execute_reply.started": "2024-02-01T06:28:10.263764Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "(1 missing value generated)\n",
      "\n",
      "(1 missing value generated)\n"
     ]
    }
   ],
   "source": [
    "gen diff_total_thousands     = D1.total_thousands\n",
    "gen diff_pct_arrivals      = D1.pct_arrivals"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Model specification"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:28:11.072557Z",
     "iopub.status.busy": "2024-02-01T06:28:11.072557Z",
     "iopub.status.idle": "2024-02-01T06:28:12.358034Z",
     "shell.execute_reply": "2024-02-01T06:28:12.355954Z",
     "shell.execute_reply.started": "2024-02-01T06:28:11.072557Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "@@@@@@@@@@@@@@@@@@@@@@@@total_thousands@@@@@@@@@@@@@@@@@@@@@@@@@\n",
      "\n",
      "Dickey–Fuller test for unit root           Number of obs  = 47\n",
      "Variable: total_thousands                  Number of lags =  0\n",
      "\n",
      "H0: Random walk without drift, d = 0\n",
      "\n",
      "                                       Dickey–Fuller\n",
      "                   Test      -------- critical value ---------\n",
      "              statistic           1%           5%          10%\n",
      "--------------------------------------------------------------\n",
      " Z(t)            -2.133       -3.600       -2.938       -2.604\n",
      "--------------------------------------------------------------\n",
      "MacKinnon approximate p-value for Z(t) = 0.2313.\n",
      "@@@@@@@@@@@@@@@@@@@@@@@@pct_arrivals@@@@@@@@@@@@@@@@@@@@@@@@@\n",
      "\n",
      "Dickey–Fuller test for unit root           Number of obs  = 47\n",
      "Variable: pct_arrivals                     Number of lags =  0\n",
      "\n",
      "H0: Random walk without drift, d = 0\n",
      "\n",
      "                                       Dickey–Fuller\n",
      "                   Test      -------- critical value ---------\n",
      "              statistic           1%           5%          10%\n",
      "--------------------------------------------------------------\n",
      " Z(t)            -2.552       -3.600       -2.938       -2.604\n",
      "--------------------------------------------------------------\n",
      "MacKinnon approximate p-value for Z(t) = 0.1034.\n",
      "\n",
      "@@@@@@@@@@@@@@@@@@@@@@@@@diff_total_thousands@@@@@@@@@@@@@@@@@@@@@@@@\n",
      "\n",
      "Dickey–Fuller test for unit root           Number of obs  = 46\n",
      "Variable: diff_total_tho~s                 Number of lags =  0\n",
      "\n",
      "H0: Random walk without drift, d = 0\n",
      "\n",
      "                                       Dickey–Fuller\n",
      "                   Test      -------- critical value ---------\n",
      "              statistic           1%           5%          10%\n",
      "--------------------------------------------------------------\n",
      " Z(t)            -6.526       -3.607       -2.941       -2.605\n",
      "--------------------------------------------------------------\n",
      "MacKinnon approximate p-value for Z(t) = 0.0000.\n",
      "@@@@@@@@@@@@@@@@@@@@@@@@@diff_pct_arrivals@@@@@@@@@@@@@@@@@@@@@@@@\n",
      "\n",
      "Dickey–Fuller test for unit root           Number of obs  = 46\n",
      "Variable: diff_pct_arriv~s                 Number of lags =  0\n",
      "\n",
      "H0: Random walk without drift, d = 0\n",
      "\n",
      "                                       Dickey–Fuller\n",
      "                   Test      -------- critical value ---------\n",
      "              statistic           1%           5%          10%\n",
      "--------------------------------------------------------------\n",
      " Z(t)            -9.163       -3.607       -2.941       -2.605\n",
      "--------------------------------------------------------------\n",
      "MacKinnon approximate p-value for Z(t) = 0.0000.\n"
     ]
    }
   ],
   "source": [
    "foreach v in total_thousands pct_arrivals  {\n",
    "\n",
    "    di \"@@@@@@@@@@@@@@@@@@@@@@@@`v'@@@@@@@@@@@@@@@@@@@@@@@@@\"  \n",
    "    dfuller `v', lags(0)\n",
    "    \n",
    "}\n",
    "\n",
    "foreach v in total_thousands pct_arrivals {\n",
    "\n",
    "    di \"@@@@@@@@@@@@@@@@@@@@@@@@@diff_`v'@@@@@@@@@@@@@@@@@@@@@@@@\"  \n",
    "    dfuller diff_`v', lags(0)\n",
    "    \n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:28:12.360031Z",
     "iopub.status.busy": "2024-02-01T06:28:12.360031Z",
     "iopub.status.idle": "2024-02-01T06:28:13.221078Z",
     "shell.execute_reply": "2024-02-01T06:28:13.220084Z",
     "shell.execute_reply.started": "2024-02-01T06:28:12.360031Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "Engle-Granger test for cointegration                  N (1st step)  =       48\n",
      "                                                      N (test)      =       47\n",
      "------------------------------------------------------------------------------\n",
      "                  Test         1% Critical       5% Critical      10% Critical\n",
      "               Statistic           Value             Value             Value\n",
      "------------------------------------------------------------------------------\n",
      " Z(t)             -2.698            -4.140            -3.469            -3.136\n",
      "\n",
      "Critical values from MacKinnon (1990, 2010)\n",
      "\n",
      "\n",
      "Breusch–Godfrey LM test for autocorrelation\n",
      "---------------------------------------------------------------------------\n",
      "    lags(p)  |          chi2               df                 Prob > chi2\n",
      "-------------+-------------------------------------------------------------\n",
      "       1     |          0.383               1                   0.5361\n",
      "       2     |          0.800               2                   0.6702\n",
      "---------------------------------------------------------------------------\n",
      "                        H0: no serial correlation\n"
     ]
    }
   ],
   "source": [
    "egranger total_thousands pct_arrivals, lags(0) // Test for cointegration\n",
    "estat bgodfrey, lags(1 2)                    // Test autocorrelation of residual in second regression > otherwise need to incorporate lags"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ECM"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Engle-Granger"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-02-01T06:28:13.224586Z",
     "iopub.status.busy": "2024-02-01T06:28:13.223584Z",
     "iopub.status.idle": "2024-02-01T06:28:14.339096Z",
     "shell.execute_reply": "2024-02-01T06:28:14.337554Z",
     "shell.execute_reply.started": "2024-02-01T06:28:13.224586Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "      Source |       SS           df       MS      Number of obs   =        48\n",
      "-------------+----------------------------------   F(1, 46)        =      7.45\n",
      "       Model |  35.1670496         1  35.1670496   Prob > F        =    0.0090\n",
      "    Residual |  217.123392        46  4.72007375   R-squared       =    0.1394\n",
      "-------------+----------------------------------   Adj R-squared   =    0.1207\n",
      "       Total |  252.290442        47  5.36788175   Root MSE        =    2.1726\n",
      "\n",
      "------------------------------------------------------------------------------\n",
      "total_thou~s | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]\n",
      "-------------+----------------------------------------------------------------\n",
      "pct_arrivals |  -4.314541   1.580669    -2.73   0.009    -7.496263   -1.132819\n",
      "       _cons |   5.318472   1.132434     4.70   0.000        3.039    7.597944\n",
      "------------------------------------------------------------------------------\n",
      "\n",
      "\n",
      "\n",
      "\n",
      "      Source |       SS           df       MS      Number of obs   =        46\n",
      "-------------+----------------------------------   F(2, 43)        =      2.43\n",
      "       Model |  8.79984433         2  4.39992217   Prob > F        =    0.0998\n",
      "    Residual |   77.780479        43  1.80884835   R-squared       =    0.1016\n",
      "-------------+----------------------------------   Adj R-squared   =    0.0599\n",
      "       Total |  86.5803233        45  1.92400718   Root MSE        =    1.3449\n",
      "\n",
      "------------------------------------------------------------------------------\n",
      "D.           |\n",
      "total_thou~s | Coefficient  Std. err.      t    P>|t|     [95% conf. interval]\n",
      "-------------+----------------------------------------------------------------\n",
      "       resid |\n",
      "         L1. |   -.203978   .0924947    -2.21   0.033    -.3905113   -.0174448\n",
      "             |\n",
      "pct_arrivals |\n",
      "         LD. |   .4205252   1.524863     0.28   0.784    -2.654654    3.495704\n",
      "             |\n",
      "       _cons |   .0395512   .1988529     0.20   0.843     -.361474    .4405764\n",
      "------------------------------------------------------------------------------\n"
     ]
    }
   ],
   "source": [
    "regress total_thousands pct_arrivals   \n",
    "cap drop resid\n",
    "predict resid, res\n",
    "\n",
    "regress D.total_thousands L.resid LD.pct_arrivals"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Stata",
   "language": "stata",
   "name": "stata"
  },
  "language_info": {
   "codemirror_mode": "stata",
   "file_extension": ".do",
   "mimetype": "text/x-stata",
   "name": "stata",
   "version": "15.1"
  },
  "toc-autonumbering": true
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
